<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:key name="gl_version" match="gl" use="@version"/>

<!-- parse root node -->
<xsl:template match="/glxml">
<xsl:text>/* gl-dynamic.c - opengl32.lib function pointer assignment */

#include "gl.h"

static HMODULE handle;

static void* _opengl_get_proc_address(const char* name)
{
	void *p;

	if (handle)	
		p = GetProcAddress(handle,name);
	else
		p = _wglGetProcAddress(name);

	return p;
}

static int init_wgl(void)
{
	/* load opengl32.dll */
	handle = LoadLibrary("opengl32.dll");
	if (handle == NULL) return 1;

#pragma warning(disable:4113)
#pragma warning(disable:4047)

	_wglGetProcAddress = GetProcAddress(handle,"wglGetProcAddress");
	_wglCreateContext = GetProcAddress(handle,"wglCreateContext");
	_wglDeleteContext = GetProcAddress(handle,"wglDeleteContext");
	_wglMakeCurrent = GetProcAddress(handle,"wglMakeCurrent");

	return 0;
}

int opengl_init_func(void)
{
  if (init_wgl() != 0) return 1;

</xsl:text>

<xsl:apply-templates select="key('gl_version','1.0')"/>
<xsl:apply-templates select="key('gl_version','1.1')"/>
<xsl:apply-templates select="key('gl_version','1.2')"/>
<xsl:apply-templates select="key('gl_version','1.3')"/>
<xsl:apply-templates select="key('gl_version','1.4')"/>
<xsl:apply-templates select="key('gl_version','1.5')"/>

<xsl:apply-templates select="glext"/>

<xsl:text>
  return 0;
}

/* eof */
</xsl:text>
</xsl:template>



<!-- opengl 1.0 -->
<xsl:template match="key('gl_version','1.0')">
<xsl:text>
  /*
   * OpenGL 1.0 API
   */

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>

<!-- opengl 1.1 -->
<xsl:template match="key('gl_version','1.1')">
<xsl:text>
  /*
   * OpenGL 1.1 API
   */

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>


<!-- opengl 1.2 -->
<xsl:template match="key('gl_version','1.2')">
<xsl:text>
  return 0;
}

int opengl_init_func2(void)
{

  /*
   * OpenGL 1.2 API
   */

  /* hack to tell we use _wglGetProcAddress from now on
   * it doesnt seem to work for 1.3 to 1.5 entrypoints
   * but we need it for the extensions later on anyway */
  handle = 0;

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>


<!-- opengl 1.3 -->
<xsl:template match="key('gl_version','1.3')">
<xsl:text>
  /*
   * OpenGL 1.3 API
   */

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>


<!-- opengl 1.4 -->
<xsl:template match="key('gl_version','1.4')">
<xsl:text>
  /*
   * OpenGL 1.4 API
   */

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>


<!-- opengl 1.5 -->
<xsl:template match="key('gl_version','1.5')">
<xsl:text>
  /*
   * OpenGL 1.5 API
   */

</xsl:text>

<xsl:apply-templates select="function"/>
</xsl:template>

<xsl:template match="glext">
<xsl:text>
  /* </xsl:text><xsl:value-of select="@name"/><xsl:text> */
</xsl:text>
<xsl:apply-templates select="function"/>
</xsl:template>



<xsl:template match="function">

<xsl:choose>
	<xsl:when test="args">
		<xsl:apply-templates select="args"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:choose>
			<xsl:when test="return">
				<xsl:text>  gl</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>  gl</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="name"/>

		<xsl:text> = _opengl_get_proc_address("gl</xsl:text>
		<xsl:value-of select="name"/>
		<xsl:text>");
</xsl:text>
	</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template match="args">
<xsl:choose>
	<xsl:when test="../return">
		<xsl:text>  gl</xsl:text>
		<xsl:value-of select="../name"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:text>  gl</xsl:text>
		<xsl:value-of select="../name"/>
	</xsl:otherwise>
</xsl:choose>
	
<xsl:if test="@suffix">
	<xsl:value-of select="@suffix"/>
</xsl:if>

<xsl:text> = _opengl_get_proc_address("gl</xsl:text>
		<xsl:value-of select="../name"/>
		<xsl:if test="@suffix">
			<xsl:value-of select="@suffix"/>
		</xsl:if>

<xsl:text>");
</xsl:text>
</xsl:template>



</xsl:stylesheet>
